let createError = require('http-errors');
let express = require('express');
let path = require('path');
let cookieParser = require('cookie-parser');
let logger = require('morgan');

let indexRouter = require('./routes/index');
let productRouter = require('./routes/product');
let usersRouter = require('./routes/users');
let orderRouter = require('./routes/order');
let zpRouter = require('./routes/zp');

let app = express();
let session = require('express-session');
let FileStore = require('session-file-store')(session);

let identityKey = 'skey';

app.use(session({
	name: identityKey,
	secret: 'chyingp', // 用来对session id相关的cookie进行签名
	store: new FileStore(), // 本地存储session（文本文件，也可以选择其他store，比如redis的）
	saveUninitialized: false, // 是否自动保存未初始化的会话，建议false
	resave: false, // 是否每次都重新保存会话，建议false
	cookie: {
		maxAge: 24*60 *60 * 1000 // 有效期，单位是毫秒
	}
}));

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

let bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/',express.static(path.join(__dirname, 'public')));



app.use('/', indexRouter);
app.use('/user', usersRouter);
app.use('/product', productRouter);
app.use('/order', orderRouter);
app.use('/zp', zpRouter);




// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;
